package com.ruoyi.consumer.controller.facilit.api;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.consumer.controller.facilit.fill.BigDataItem;
import com.ruoyi.consumer.controller.facilit.fill.FiaciltMode;
import com.ruoyi.consumer.controller.facilit.fill.FiaciltParameter;
import com.ruoyi.consumer.domain.order.PayOrderDTO;
import com.ruoyi.consumer.utils.JsonUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import static com.ruoyi.consumer.constance.ApiConstance.URL_PREFIX;

@Service
public class APIServiceImpl implements APIService {



    @Override
    public String getAuthorization(String appKey, String appSecret) {
        return HttpUtils.sendGet(URL_PREFIX + "oauth/authorize", "appKey=" + appKey + "&" + "appSecret=" + appSecret);
    }

    @Override
    public String getEquipmentInfoBySn(String sn, String token) throws IOException {
        return HttpUtils.sendGetOauth(URL_PREFIX + "manage/getEquipmentInfoBySn", "sn=" + sn, Constants.UTF8, token);
    }

    @Override
    public String getEquipmentStockBySn(String sn, String token) throws IOException {
        return HttpUtils.sendGetOauth(URL_PREFIX + "manage/getEquipmentStockBySn", "sn=" + sn, Constants.UTF8, token);
    }

    @Override
    public String findByOrderId(Long orderId, String token) throws IOException {
        return HttpUtils.sendGetOauth(URL_PREFIX + "order/findByOrderId", "orderId=" + orderId, Constants.UTF8, token);
    }

    @Override
    public String findOrderDetailByOrderId(Long orderId, String token) throws IOException {
        return HttpUtils.sendGetOauth(URL_PREFIX + "orderDetail/findOrderDetailByOrderId", "orderId=" + orderId, Constants.UTF8, token);
    }

    @Override
    public String payOrder(PayOrderDTO payOrderDTO, String token) {
//        将payOrderDTO转为json
        String payOrderDTOJson = JsonUtils.serialize(payOrderDTO);
//        System.out.println(payOrderDTOJson);
        return HttpUtils.sendPostOauth(URL_PREFIX + "payAmountCome", payOrderDTOJson, token);
    }

    @Override
    public Boolean isWorking(BigDataItem bigDataItem) {
        System.out.println(bigDataItem.getOrderComeStatus());
        if (bigDataItem.getOrderComeStatus().equals("ing"))
            return true;
        else
            return false;
    }

    @Override
    public Boolean isWaiting(String sn, String token) throws IOException {
        String equipment = HttpUtils.sendGetOauth(URL_PREFIX + "manage/getEquipmentInfoBySn", "sn=" + sn, Constants.UTF8, token);

        Map<String, Object> heartbeat = null;
        try {

            ObjectMapper mapper = new ObjectMapper();

            // 解析 JSON 字符串为 Map
            Map<String, Object> jsonMap = mapper.readValue(equipment, Map.class);

            // 获取 datas 对象
            Map<String, Object> datas = (Map<String, Object>) jsonMap.get("datas");

            // 获取 manage 对象
            if (datas != null) {
                heartbeat = (Map<String, Object>) datas.get("heartbeat");

                // 现在 manage 包含您需要的信息
//                System.out.println(heartbeat);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Integer status = (Integer) heartbeat.get("equipmentMachineState");
        if (status == 1) {
            return true;
        } else
            return false;
    }

    @Override
    public String setFaciltStatus(FiaciltParameter fiaciltParameter, String token) {
        String FaciltParameter = JsonUtils.serialize(fiaciltParameter);
        return HttpUtils.sendPostOauth(URL_PREFIX + "equipment/updateEquipmentInfo", FaciltParameter, token);
    }

    @Override
    public String setFaciltMode(FiaciltMode fiaciltMode, String token) {
        String FaciltParameter = JsonUtils.serialize(fiaciltMode);
        return HttpUtils.sendPostOauth(URL_PREFIX + "equipment/updateEquipmentInfo", FaciltParameter, token);
    }
}
